import { GetListsAsync,GetById } from '@/services/article';
import { Reducer, Effect } from 'umi';

export interface ArtModelType {
    namespace: string;
    state: any;
    effects: {
        getListsAsync: Effect;
        getById: Effect;        
        setCurrent: Effect;
    };
    reducers: {
        deal: Reducer<any>;
        set: Reducer<any>;
    };
}

const ArticleModel: ArtModelType = {
    namespace: 'Article',
    state: {
        current: {},
        response: {},
    },
    effects: {
        * getListsAsync({ payload }, { call, put }) {
            const response = yield call(GetListsAsync, payload);
            yield put({
                type: 'deal',
                payload: response,
            });
        },
        * getById({ payload }, { call, put }) {
            const response = yield call(GetById, payload);
            yield put({
                type: 'deal',
                payload: response,
            });
        },
        * setCurrent({ payload }, { put }) {
            yield put({
                type: 'set',
                payload,
            });
        },
    },
    reducers: {
        set(state, action) {
            return { ...state, current: action.payload };
        },
        deal(state, action) {
            return { ...state, response: action.payload };
        },
    },
};
export default ArticleModel;